package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.DataDictoryCatalogMapper;
import com.todod.model.DataDictoryCatalog;
import com.todod.model.DataDictoryFile;
import com.todod.service.IDataDictoryCatalogService;
import com.todod.service.IDataDictoryFileService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dyl
 * @date 2022-10-05
 */
@Service
@Transactional
public class DataDictoryCatalogServiceImpl extends ServiceImpl<DataDictoryCatalogMapper, DataDictoryCatalog>
		implements IDataDictoryCatalogService {
	@Autowired
	private DataDictoryCatalogMapper dataDictoryCatalogMapper;
	@Autowired
	private IDataDictoryFileService dataDictoryFileService;

	@Override
	public List<DataDictoryCatalog> getListByXmId(Long xiangmuId) {

		QueryWrapper<DataDictoryCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_xm_id", xiangmuId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<DataDictoryCatalog> list = dataDictoryCatalogMapper.selectList(queryWrapper);

		return list;
	}

	@Override
	public void saveData(Long _userId, DataDictoryCatalog dataDictoryCatalog) {

		dataDictoryCatalog.setId(SnowflakeIdGenerator.getId());

		dataDictoryCatalog.setCreatetimeTi(LocalDateTime.now());
		dataDictoryCatalog.setCreateuserId(_userId);
		dataDictoryCatalogMapper.insert(dataDictoryCatalog);
	}

	@Override
	public void editData(Long _userId, DataDictoryCatalog dataDictoryCatalog) {

		DataDictoryCatalog dataDictoryCatalog1 = dataDictoryCatalogMapper.selectById(dataDictoryCatalog.getId());

		dataDictoryCatalog.setCreatetimeTi(dataDictoryCatalog1.getCreatetimeTi());
		dataDictoryCatalog.setCreateuserId(dataDictoryCatalog1.getCreateuserId());
		dataDictoryCatalog.setModifytimeTi(LocalDateTime.now());
		dataDictoryCatalog.setModifyuserId(_userId);
		dataDictoryCatalogMapper.updateById(dataDictoryCatalog);
	}

	@Override
	public Object getInfo(Long code) {
		Map<String, Object> map = new HashMap<>();
		DataDictoryCatalog dataDictoryCatalog = dataDictoryCatalogMapper.getInfo(code);

		map.put("dataDictoryCatalog", dataDictoryCatalog);
		return map;
	}

	@Override
	public List<DataDictoryCatalog> getListByParentId(Long xiangmuId) {

		QueryWrapper<DataDictoryCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_parent_id", xiangmuId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<DataDictoryCatalog> list = dataDictoryCatalogMapper.selectList(queryWrapper);

		return list;
	}

	@Override
	public List<DataDictoryCatalog> getListByUserIdAndType(Long _userId, Long xmId) {
		List<DataDictoryCatalog> returnList = new ArrayList<DataDictoryCatalog>();

		DataDictoryCatalog dataDictoryCatalog = new DataDictoryCatalog();
		dataDictoryCatalog.setId(xmId);
		dataDictoryCatalog.setParentId(-1L);
		dataDictoryCatalog.setSortNum(0);
		dataDictoryCatalog.setName("数据库设计目录");
		dataDictoryCatalog.setFlag("ML");
		dataDictoryCatalog.setXmId(xmId);

		returnList.add(dataDictoryCatalog);

		QueryWrapper<DataDictoryCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_xm_id", xmId);
		queryWrapper.eq("dm_parent_id", xmId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<DataDictoryCatalog> list = dataDictoryCatalogMapper.selectList(queryWrapper);

		for (DataDictoryCatalog dataDictoryCatalog1 : list) {
			dataDictoryCatalog1.setFlag("ML");
			returnList.add(dataDictoryCatalog1);
		}

		return returnList;
	}

	@Override
	public List<DataDictoryCatalog> getListByUserIdAndTypeAndParentId(Long _userId, Long xmId, Long parentId) {
		List<DataDictoryCatalog> returnList = new ArrayList<DataDictoryCatalog>();

		QueryWrapper<DataDictoryCatalog> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dm_xm_id", xmId);
		queryWrapper.eq("dm_parent_id", parentId);
		queryWrapper.orderByAsc("dm_sort_num");

		List<DataDictoryCatalog> list = dataDictoryCatalogMapper.selectList(queryWrapper);

		if (list != null && list.size() > 0) {
			for (DataDictoryCatalog dataDictoryCatalog : list) {
				dataDictoryCatalog.setFlag("ML");
				returnList.add(dataDictoryCatalog);
			}
		} else {// 获取个人创建数据
			List<DataDictoryFile> plist = dataDictoryFileService.getListByClassifyId(parentId);
			for (DataDictoryFile dataDictoryFile : plist) {
				DataDictoryCatalog dataDictoryCatalog = new DataDictoryCatalog();
				dataDictoryCatalog.setId(dataDictoryFile.getId());
				dataDictoryCatalog.setParentId(parentId);
				dataDictoryCatalog.setSortNum(dataDictoryFile.getSortNum());
				dataDictoryCatalog.setName(dataDictoryFile.getTableName());
				dataDictoryCatalog.setFlag("WD");
				dataDictoryCatalog.setXmId(xmId);

				returnList.add(dataDictoryCatalog);
			}
		}

		return returnList;
	}

}
